package com.whfc.ms.api.quality.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.fuum.dto.SysUserDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.ms.api.quality.service.MsQualityCheckService;
import com.whfc.quality.dto.*;
import com.whfc.quality.param.QualityCheckListParam;
import com.whfc.quality.param.QualityCheckLogParam;
import com.whfc.quality.param.QualityCheckParam;
import com.whfc.quality.param.QualityCheckUserParam;
import com.whfc.quality.service.QualityCheckService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClasssName MsQualityCheckServiceImpl
 * @Description 质量管理问题上报实现类
 * @Author hw
 * @Date 2020/8/3 11:01
 * @Version 1.0
 */
@Service
public class MsQualityCheckServiceImpl implements MsQualityCheckService {

    @DubboReference(interfaceClass = QualityCheckService.class, version = "1.0.0")
    private QualityCheckService qualityCheckService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    /**
     * 质量图片路径
     */
    private String qualityImgOssDir = "quality/img";

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    @Override
    public QualityCheckListDTO getCheckList(Integer deptId, Integer[] type, Integer[] state, Date startTime,
                                            Date endTime, Integer partId, Integer urgency, Integer overdue, Integer userId, Integer pageNum, Integer pageSize) {
        QualityCheckListParam checkListParam = new QualityCheckListParam();
        checkListParam.setUserId(userId);
        checkListParam.setType(type == null || type.length <= 0 ? null : type);
        checkListParam.setDeptId(deptId);
        checkListParam.setEndTime(endTime);
        if (overdue != null) {
            checkListParam.setOverdue(new Integer[]{overdue});
        }
        checkListParam.setPartId(partId);
        checkListParam.setStartTime(startTime);
        checkListParam.setState(state == null || state.length <= 0 ? null : state);
        if (urgency != null) {
            checkListParam.setUrgency(new Integer[]{urgency});
        }
        checkListParam.setPageSize(pageSize);
        checkListParam.setPageNum(pageNum);
        return qualityCheckService.list(checkListParam);
    }

    @Override
    public QualityCheckDescDTO desc(Integer deptId) {
        return qualityCheckService.desc(deptId);
    }

    @Override
    public void add(QualityCheckParam checkParam, SysUser user) {
        QualityCheckUserParam userParam = new QualityCheckUserParam();
        userParam.setType(1);
        userParam.setUserId(user.getId());
        userParam.setUserName(user.getNickname());
        userParam.setUserPhone(user.getPhone());
        checkParam.getCheckUsers().add(userParam);
        List<QualityCheckUserParam> qualityCheckUserParams = checkParam.getCheckUsers().stream().filter(Objects::nonNull).collect(Collectors.toList());
        Collections.sort(qualityCheckUserParams);
        
        checkParam.setCheckUsers(qualityCheckUserParams);
        checkParam.setCheckUserId(user.getId());
        checkParam.setCheckUserName(user.getNickname());
        qualityCheckService.addCheck(checkParam);
    }

    @Override
    public void del(Integer checkId) {
        qualityCheckService.delCheck(checkId);
    }

    @Override
    public List<SysUserDTO> sysUserList(Integer deptId) {
        return sysUserService.getUsersByDeptId(deptId);
    }

    @Override
    public void addLog(QualityCheckLogParam qualityCheckLogParam) {
        qualityCheckService.addCheckLog(qualityCheckLogParam);
    }

    @Override
    public QualityCheckStatisticalDTO statistics(Integer deptId, Integer userId, Date startTime, Date endTime) {
        return qualityCheckService.statistical(deptId, userId, startTime, endTime, 3);
    }

    @Override
    public QualityCheckStatisticalDTO statisticsDay(Integer deptId, Integer userId, Date dayTime) {
        Date startTime = DateUtil.getDateBegin(dayTime);
        Date endTime = DateUtil.getDateEnd(dayTime);
        return qualityCheckService.statistical(deptId, userId, startTime, endTime, 1);
    }

    @Override
    public QualityCheckStatisticalDTO statisticsMonth(Integer deptId, Integer userId, Date monthTime) {
        Date startTime = DateUtil.getMonthBegin(monthTime);
        Date endTime = DateUtil.getMonthEnd(monthTime);
        return qualityCheckService.statistical(deptId, userId, startTime, endTime, 2);
    }

    @Override
    public String uploadImg(MultipartFile file) {
        try {
            String fileName = RandomUtil.getRandomFileName() + ".png";
            InputStream inputStream = file.getInputStream();
            String ossKey = filePathConfig.getFilePath(qualityImgOssDir, fileName);
            return fileHandler.upload(ossKey, inputStream);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public QualityLogDTO qualityLog(Integer deptId, Date date) {
        return qualityCheckService.qualityLog(deptId, date);
    }

    @Override
    public void export(String qualityIds, Integer deptId) {
        String[] strIds = qualityIds.split(",");
        List<Integer> intIds = Arrays.stream(strIds).map(Integer::valueOf).collect(Collectors.toList());
        qualityCheckService.qualityExport(intIds, deptId);
    }

    @Override
    public QualityCheckScreenDTO qualityCheckScreenStatistical(Integer deptId, Date startTime, Date endTime) {
        return qualityCheckService.qualityCheckScreenStatistical(deptId, startTime, endTime);
    }

    @Override
    public QualityCheckScreenDTO qualityCheckTypeAnalysis(Integer deptId, Date startTime, Date endTime) {
        return qualityCheckService.qualityCheckTypeAnalysis(deptId, startTime, endTime);
    }

    @Override
    public QualityCheckUnfinishedDTO qualityCheckUnfinished(Integer deptId, Date startTime, Date endTime) {
        return qualityCheckService.qualityCheckUnfinished(deptId, startTime, endTime);
    }

    @Override
    public ListData<QualityCheckPartStatisticalDTO> qualityCheckPartAnalysis(Integer deptId, Date startTime, Date endTime) {
        List<QualityCheckPartStatisticalDTO> list = qualityCheckService.qualityCheckPartAnalysis(deptId, startTime, endTime);
        return new ListData<>(list);
    }

    @Override
    public void deleteBatch(List<Integer> idList) {
        qualityCheckService.deleteBatch(idList);
    }
}
